﻿<div class="layui-fluid layui-anim febs-anim" id="febs-projectType" lay-title="业务类型管理">
    <div class="layui-row layui-col-space8 febs-container">
        <div class="layui-col-md7 layui-col-sm6 layui-col-xs12">
            <div class="layui-card">
                <div class="layui-card-body febs-table-full">
                    <form class="layui-form layui-table-form" lay-filter="projectType-table-form" id="projectType-table-form">
                        <div class="layui-row">
                            <div class="layui-col-md9 layui-col-sm9 layui-col-xs9">
                                <div class="layui-form-item">
                                    <div class="layui-inline">
                                        <label class="layui-form-label layui-form-label-sm">业务类型</label>
                                        <div class="layui-input-inline">
                                            <input type="text" name="projectType" autocomplete="off" class="layui-input">
                                        </div>
                                    </div>
                                </div>
                            </div>
                            <div class="layui-col-md3 layui-col-sm12 layui-col-xs12 table-action-area">
                                <div class="layui-btn layui-btn-sm layui-btn-primary febs-button-blue-plain  table-action" id="query">
                                    <i class="layui-icon">&#xe848;</i>
                                </div>
                                <div class="layui-btn layui-btn-sm layui-btn-primary febs-button-green-plain  table-action" id="reset">
                                    <i class="layui-icon">&#xe79b;</i>
                                </div>
                                <div class="layui-btn layui-btn-sm layui-btn-primary table-action action-more"
                                     shiro:hasAnyPermissions="projectType:add,projectType:delete">
                                    <i class="layui-icon">&#xe875;</i>
                                </div>
                            </div>
                        </div>
                    </form>
                    <table lay-filter="projectTypeTable" lay-data="{id: 'projectTypeTable'}"></table>
                </div>
            </div>
        </div>
        <div class="layui-col-md5 layui-col-sm6 layui-col-xs12">
            <div class="layui-card">
                <div class="layui-card-header" id="form-header">新增类型</div>
                <div class="layui-card-body febs-table-full">
                    <form class="layui-form layui-table-form" action="" lay-filter="projectType-form">
                        <div class="layui-form-item febs-hide">
                            <label class="layui-form-label febs-form-item-require">类型ID：</label>
                            <div class="layui-input-block">
                                <input type="text" name="id" class="layui-input">
                            </div>
                        </div>
                        <div class="layui-form-item">
                            <label class="layui-form-label febs-form-item-require">业务类型：</label>
                            <div class="layui-input-block">
                                <input type="text" name="projectType" autocomplete="off" class="layui-input" minlength="1"
                                       lay-verify="range">
                            </div>
                        </div>
                        <div class="layui-form-item febs-hide">
                            <button type="reset" class="layui-btn" id="reset-form"></button>
                            <button class="layui-btn" lay-submit="" lay-filter="projectType-form-submit"
                                    id="submit-form"></button>
                        </div>
                    </form>
                </div>
                <div class="layui-card-footer">
                    <button class="layui-btn" id="submit">保存</button>
                </div>
            </div>
        </div>
    </div>
</div>
<script type="text/html" id="projectType-option">
<!--    <span shiro:lacksPermission="projectType:update,projectType:delete">-->
<!--        <span class="layui-badge-dot febs-bg-orange"></span> 无权限-->
<!--    </span>-->
    <a lay-event="edit" ><i class="layui-icon febs-edit-area febs-blue">&#xe7a4;</i></a>
    <a lay-event="del"  ><i class="layui-icon febs-edit-area febs-red">&#xe7f9;</i></a>
</script>
<script data-th-inline="none" type="text/javascript">
    layui.use(['dropdownMenu', 'jquery', 'laydate', 'febs', 'form', 'eleTree', 'table', 'validate', 'xmSelect', 'layer'], function () {
        let $ = layui.jquery,
            laydate = layui.laydate,
            febs = layui.febs,
            form = layui.form,
            table = layui.table,
            dropdownMenu = layui.dropdownMenu,
            validate = layui.validate,
            $view = $('#febs-projectType'),
            $query = $view.find('#query'),
            $reset = $view.find('#reset'),
            $submit = $view.find('#submit'),
            $searchForm = $view.find('#projectType-table-form'),
            $header = $view.find('#form-header'),
            layer = layui.layer,
            companyXmSelect,
            tableIns;

        form.verify(validate);
        form.render();

        initTable();

        laydate.render({
            elem: '#createTime',
            range: true
        });

        companyXmSelect = xmSelect.render({
            el: '#select-company',
            radio: true,
            clickClose: true,
            name: 'companyId',
            model: {label: {type: 'text'}}
        })

        febs.get(ctx + 'company', null, function (result) {
            let companyList = result.data;
            let data = [];
            for (let i = 0; i < companyList.length; i++) {
                let e = {};
                e.value = companyList[i].id;
                e.name = companyList[i].companyName;
                data[i] = e;
            }
            companyXmSelect.update(
                {data: data}
            )
        })


        dropdownMenu.render({
            elem: $view.find('.action-more'),
            click: function (name, elem, event) {
                let checkStatus = table.checkStatus('projectTypeTable');
                if (name === 'add') {
                    resetprojectTypeForm();
                    febs.alert.info("请在表单中填写相关信息");
                }
                if (name === 'delete') {
                    if (!checkStatus.data.length) {
                        febs.alert.warn('请选择需要删除的类型');
                    } else {
                        febs.modal.confirm('删除类型', '确定删除所选类型？', function () {
                            let projectTypeIds = [];
                            layui.each(checkStatus.data, function (key, item) {
                                projectTypeIds.push(item.id);
                            });
                            deleteprojectTypes(projectTypeIds.join(','));
                        });
                    }
                }
            },
            options: [{
                name: 'add',
                title: '新增类型',
                perms: 'projectType:add'
            }, {
                name: 'delete',
                title: '删除类型',
                perms: 'projectType:delete'
            }]
        });

        table.on('tool(projectTypeTable)', function (obj) {
            let data = obj.data,
                layEvent = obj.event;
            console.log(data)
            if (layEvent === 'edit') {
                $header.text('修改类型');
                form.val("projectType-form", {
                    "id": data.id,
                    "projectType": data.projectType,
                });
                companyXmSelect.setValue([data.companyId])
            }
            if (layEvent === 'del') {
                febs.modal.confirm('删除类型', '确定删除该类型？', function () {
                    deleteprojectTypes(data.id);
                });
            }
            if (layEvent === 'main_subject') {
                febs.modal.open('设置主科目', 'accounting/projectType/mainSubject', {
                    //传递参数，弹出的页面可以使用 POPUP_DATA.username 获取值
                    maxmin: false,
                    data: {
                        projectTypeId: data.id
                    }
                });
            }
        });

        $query.on('click', function () {
            resetprojectTypeForm();
            tableIns.reload({where: getQueryParams(), page: {curr: 1}});
        });

        $reset.on('click', function () {
            resetprojectTypeForm();
            $searchForm[0].reset();
            tableIns.reload({where: getQueryParams(), page: {curr: 1}});
        });

        $submit.on('click', function () {
            let data = form.val('projectType-form')
            if (data.manualTax === undefined || data.manualTax === '') {
                data.manualTax = 0
            }
            console.log(data)
            if (data.projectType === '') {
                febs.alert.error('请填写业务类型')
            }  else {
                addOrUpdateprojectType(data)
            }
        });

        function initTable() {
            tableIns = febs.table.init({
                elem: $view.find('table'),
                id: 'projectTypeTable',
                url: ctx + 'projectType/list',
                cols: [[
                    {type: 'checkbox'},
                    {title: '操作', toolbar: '#projectType-option', width: 80},
                    {field: 'projectType', title: '业务类型', minWidth: 160},
                    {field: 'createTime', title: '创建时间', minWidth: 160}
                ]]
            });
        }

        function getQueryParams() {
            let params = $searchForm.serializeJson();
            params.invalidate_ie_cache = new Date();
            return params;
        }

        function resetprojectTypeForm() {
            $view.find('#reset-form').trigger('click');
            $header.text('新增类型');
        }

        form.on('submit(projectType-form-submit)', function (data) {
            if (data.field.manualTax === undefined) {
                data.field.manualTax = 0
            }
            addOrUpdateprojectType(data.field);
        });

        function deleteprojectTypes(projectTypeIds) {
            febs.get(ctx + 'projectType/delete/' + projectTypeIds, null, function () {
                febs.alert.success('删除类型成功');
                $query.trigger('click');
            })
        }

        let addOrUpdateprojectType = function (data) {
            if (data.id && $header.text() === '修改类型') {
                febs.post(ctx + 'projectType/update', data, function (result) {
                    if (result.code === 200) {
                        febs.alert.success('修改类型成功');
                        $query.trigger('click');
                    } else {
                        febs.alert.fail(result.message)
                    }
                });
            } else {
                febs.post(ctx + 'projectType', data, function (result) {
                    if (result.code === 200) {
                        febs.alert.success('新增类型成功');
                        $query.trigger('click');
                    } else {
                        febs.alert.fail(result.message)
                    }
                });
            }
        }
    });
</script>